﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommonClasses
{
    public class WarGame : Game
    {
        private Deck _warDeck = new Deck();
        public Deck warDeck
        {
            get
            {
                return _warDeck;
            }
            set
            {
                _warDeck = value;
            }
        }


        private WarHand _dealerHand = new WarHand();
        public WarHand dealerHand
        {
            get
            {
                return _dealerHand;
            }
            set
            {
                _dealerHand = value;
            }
        }

        private WarHand _playerHand = new WarHand();
        public WarHand playerHand 
        {
            get
            {
                return _playerHand;
            }
            set
            {
                _playerHand = value;
            }
        }


        public delegate void War(string z);
        public delegate void Win(string x);
        public delegate void Lose(string y);
        public event War Battle;
        public event Win Winner;
        public event Lose Loser;



        public override void Deal()
        {
            playerHand.CardsOnHand.Clear();
            dealerHand.CardsOnHand.Clear();
            warDeck.initialize_cards();

            for (int w = 0; w < 26; w++)
            {
                dealerHand.CardsOnHand.Add(warDeck.GetCard());
                playerHand.CardsOnHand.Add(warDeck.GetCard());

            }

            if (dealerHand.CardsOnHand[0].FaceValue != playerHand.CardsOnHand[0].FaceValue)
            {
                dealerHand.CardsOnHand[0].FaceDown = true;
                playerHand.CardsOnHand[0].FaceDown = true;
                dealerHand.CardsOnHand[1].FaceDown = false;
                playerHand.CardsOnHand[1].FaceDown = false;
                dealerHand.CardsOnHand[2].FaceDown = false;
                playerHand.CardsOnHand[2].FaceDown = false;
                dealerHand.CardsOnHand[3].FaceDown = false;
                playerHand.CardsOnHand[3].FaceDown = false;
                dealerHand.CardsOnHand[4].FaceDown = false;
                playerHand.CardsOnHand[4].FaceDown = false;
            }
            else
            {
                dealerHand.CardsOnHand[0].FaceDown = true;
                playerHand.CardsOnHand[0].FaceDown = true;
                dealerHand.CardsOnHand[1].FaceDown = true;
                playerHand.CardsOnHand[1].FaceDown = true;
                dealerHand.CardsOnHand[2].FaceDown = true;
                playerHand.CardsOnHand[2].FaceDown = true;
                dealerHand.CardsOnHand[3].FaceDown = true;
                playerHand.CardsOnHand[3].FaceDown = true;
                dealerHand.CardsOnHand[4].FaceDown = true;
                playerHand.CardsOnHand[4].FaceDown = true;
            }
        }

        public override void PlayerMove()
        {

            if (dealerHand.CardsOnHand[0].FaceValue > playerHand.CardsOnHand[0].FaceValue)
            {
                dealerHand.CardsOnHand.Add(playerHand.CardsOnHand[0]);
                playerHand.CardsOnHand.Remove(playerHand.CardsOnHand[0]);
                dealerHand.CardsOnHand.Add(dealerHand.CardsOnHand[0]);
                dealerHand.CardsOnHand.Remove(dealerHand.CardsOnHand[0]);
                playerHand.calculate_total();
                if (dealerHand.CardsOnHand[0].FaceValue != playerHand.CardsOnHand[0].FaceValue)
                {
                    dealerHand.CardsOnHand[0].FaceDown = true;
                    playerHand.CardsOnHand[0].FaceDown = true;
                    dealerHand.CardsOnHand[1].FaceDown = false;
                    playerHand.CardsOnHand[1].FaceDown = false;
                    dealerHand.CardsOnHand[2].FaceDown = false;
                    playerHand.CardsOnHand[2].FaceDown = false;
                    dealerHand.CardsOnHand[3].FaceDown = false;
                    playerHand.CardsOnHand[3].FaceDown = false;
                    dealerHand.CardsOnHand[4].FaceDown = false;
                    playerHand.CardsOnHand[4].FaceDown = false;
                }
                else
                {
                    dealerHand.CardsOnHand[0].FaceDown = true;
                    playerHand.CardsOnHand[0].FaceDown = true;
                    dealerHand.CardsOnHand[1].FaceDown = true;
                    playerHand.CardsOnHand[1].FaceDown = true;
                    dealerHand.CardsOnHand[2].FaceDown = true;
                    playerHand.CardsOnHand[2].FaceDown = true;
                    dealerHand.CardsOnHand[3].FaceDown = true;
                    playerHand.CardsOnHand[3].FaceDown = true;
                    dealerHand.CardsOnHand[4].FaceDown = true;
                    playerHand.CardsOnHand[4].FaceDown = true;
                }
                return;
            }
            else if (dealerHand.CardsOnHand[0].FaceValue < playerHand.CardsOnHand[0].FaceValue)
            {
                playerHand.CardsOnHand.Add(dealerHand.CardsOnHand[0]);
                dealerHand.CardsOnHand.Remove(dealerHand.CardsOnHand[0]);
                playerHand.CardsOnHand.Add(playerHand.CardsOnHand[0]);
                playerHand.CardsOnHand.Remove(playerHand.CardsOnHand[0]);
                playerHand.calculate_total();
                if (dealerHand.CardsOnHand[0].FaceValue != playerHand.CardsOnHand[0].FaceValue)
                {
                    dealerHand.CardsOnHand[0].FaceDown = true;
                    playerHand.CardsOnHand[0].FaceDown = true;
                    dealerHand.CardsOnHand[1].FaceDown = false;
                    playerHand.CardsOnHand[1].FaceDown = false;
                    dealerHand.CardsOnHand[2].FaceDown = false;
                    playerHand.CardsOnHand[2].FaceDown = false;
                    dealerHand.CardsOnHand[3].FaceDown = false;
                    playerHand.CardsOnHand[3].FaceDown = false;
                    dealerHand.CardsOnHand[4].FaceDown = false;
                    playerHand.CardsOnHand[4].FaceDown = false;
                }
                else
                {
                    dealerHand.CardsOnHand[0].FaceDown = true;
                    playerHand.CardsOnHand[0].FaceDown = true;
                    dealerHand.CardsOnHand[1].FaceDown = true;
                    playerHand.CardsOnHand[1].FaceDown = true;
                    dealerHand.CardsOnHand[2].FaceDown = true;
                    playerHand.CardsOnHand[2].FaceDown = true;
                    dealerHand.CardsOnHand[3].FaceDown = true;
                    playerHand.CardsOnHand[3].FaceDown = true;
                    dealerHand.CardsOnHand[4].FaceDown = true;
                    playerHand.CardsOnHand[4].FaceDown = true;
                }
                return;
            }

            for (int a = 0; dealerHand.CardsOnHand[a].FaceValue == playerHand.CardsOnHand[a].FaceValue; a = a + 0)
            {
                if (dealerHand.CardsOnHand[a].FaceValue == playerHand.CardsOnHand[a].FaceValue &&
                   (dealerHand.CardsOnHand.Count() < a + 4 || playerHand.CardsOnHand.Count() < a + 4))
                {
                    if (dealerHand.CardsOnHand.Count() < playerHand.CardsOnHand.Count())
                    {
                        if (Winner != null) Winner("You win the war!");
                    }
                    else 
                    {
                        if (Loser != null) Loser("You lose the war!");
                    }
                    return;
                }

                if (dealerHand.CardsOnHand[a].FaceValue == playerHand.CardsOnHand[a].FaceValue &&
                   (dealerHand.CardsOnHand.Count() > a + 4 || playerHand.CardsOnHand.Count() > a + 4))
                {
                    if (Battle != null) Battle("Battle!");
                }

                a = a + 4;

                if (dealerHand.CardsOnHand[a].FaceValue > playerHand.CardsOnHand[a].FaceValue)
                {
                    for (int r = a; r != -1; r--)
                    {
                        dealerHand.CardsOnHand.Add(playerHand.CardsOnHand[r]);
                        playerHand.CardsOnHand.Remove(playerHand.CardsOnHand[r]);
                        dealerHand.CardsOnHand.Add(dealerHand.CardsOnHand[r]);
                        dealerHand.CardsOnHand.Remove(dealerHand.CardsOnHand[r]);
                    }
                    if (dealerHand.CardsOnHand[0].FaceValue != playerHand.CardsOnHand[0].FaceValue)
                    {
                        dealerHand.CardsOnHand[0].FaceDown = true;
                        playerHand.CardsOnHand[0].FaceDown = true;
                        dealerHand.CardsOnHand[1].FaceDown = false;
                        playerHand.CardsOnHand[1].FaceDown = false;
                        dealerHand.CardsOnHand[2].FaceDown = false;
                        playerHand.CardsOnHand[2].FaceDown = false;
                        dealerHand.CardsOnHand[3].FaceDown = false;
                        playerHand.CardsOnHand[3].FaceDown = false;
                        dealerHand.CardsOnHand[4].FaceDown = false;
                        playerHand.CardsOnHand[4].FaceDown = false;
                    }
                    else
                    {
                        dealerHand.CardsOnHand[0].FaceDown = true;
                        playerHand.CardsOnHand[0].FaceDown = true;
                        dealerHand.CardsOnHand[1].FaceDown = true;
                        playerHand.CardsOnHand[1].FaceDown = true;
                        dealerHand.CardsOnHand[2].FaceDown = true;
                        playerHand.CardsOnHand[2].FaceDown = true;
                        dealerHand.CardsOnHand[3].FaceDown = true;
                        playerHand.CardsOnHand[3].FaceDown = true;
                        dealerHand.CardsOnHand[4].FaceDown = true;
                        playerHand.CardsOnHand[4].FaceDown = true;
                    }
                    return;
                }
                if (dealerHand.CardsOnHand[a].FaceValue < playerHand.CardsOnHand[a].FaceValue)
                {
                    for (int r = a; r != -1; r--)
                    {
                        playerHand.CardsOnHand.Add(dealerHand.CardsOnHand[r]);
                        dealerHand.CardsOnHand.Remove(dealerHand.CardsOnHand[r]);
                        playerHand.CardsOnHand.Add(playerHand.CardsOnHand[r]);
                        playerHand.CardsOnHand.Remove(playerHand.CardsOnHand[r]);
                    }
                    if (dealerHand.CardsOnHand[0].FaceValue != playerHand.CardsOnHand[0].FaceValue)
                    {
                        dealerHand.CardsOnHand[0].FaceDown = true;
                        playerHand.CardsOnHand[0].FaceDown = true;
                        dealerHand.CardsOnHand[1].FaceDown = false;
                        playerHand.CardsOnHand[1].FaceDown = false;
                        dealerHand.CardsOnHand[2].FaceDown = false;
                        playerHand.CardsOnHand[2].FaceDown = false;
                        dealerHand.CardsOnHand[3].FaceDown = false;
                        playerHand.CardsOnHand[3].FaceDown = false;
                        dealerHand.CardsOnHand[4].FaceDown = false;
                        playerHand.CardsOnHand[4].FaceDown = false;
                    }
                    else
                    {
                        dealerHand.CardsOnHand[0].FaceDown = true;
                        playerHand.CardsOnHand[0].FaceDown = true;
                        dealerHand.CardsOnHand[1].FaceDown = true;
                        playerHand.CardsOnHand[1].FaceDown = true;
                        dealerHand.CardsOnHand[2].FaceDown = true;
                        playerHand.CardsOnHand[2].FaceDown = true;
                        dealerHand.CardsOnHand[3].FaceDown = true;
                        playerHand.CardsOnHand[3].FaceDown = true;
                        dealerHand.CardsOnHand[4].FaceDown = true;
                        playerHand.CardsOnHand[4].FaceDown = true;
                    }
                    return;
                }
                if (dealerHand.CardsOnHand[0].FaceValue != playerHand.CardsOnHand[0].FaceValue)
                {
                    dealerHand.CardsOnHand[0].FaceDown = true;
                    playerHand.CardsOnHand[0].FaceDown = true;
                    dealerHand.CardsOnHand[1].FaceDown = false;
                    playerHand.CardsOnHand[1].FaceDown = false;
                    dealerHand.CardsOnHand[2].FaceDown = false;
                    playerHand.CardsOnHand[2].FaceDown = false;
                    dealerHand.CardsOnHand[3].FaceDown = false;
                    playerHand.CardsOnHand[3].FaceDown = false;
                    dealerHand.CardsOnHand[4].FaceDown = false;
                    playerHand.CardsOnHand[4].FaceDown = false;
                }
                else
                {
                    dealerHand.CardsOnHand[0].FaceDown = true;
                    playerHand.CardsOnHand[0].FaceDown = true;
                    dealerHand.CardsOnHand[1].FaceDown = true;
                    playerHand.CardsOnHand[1].FaceDown = true;
                    dealerHand.CardsOnHand[2].FaceDown = true;
                    playerHand.CardsOnHand[2].FaceDown = true;
                    dealerHand.CardsOnHand[3].FaceDown = true;
                    playerHand.CardsOnHand[3].FaceDown = true;
                    dealerHand.CardsOnHand[4].FaceDown = true;
                    playerHand.CardsOnHand[4].FaceDown = true;
                }
                return;
            }

            //throw new NotImplementedException();
        }

        public override void Evaluate()
        {
            playerHand.calculate_total();

            //throw new NotImplementedException();
        }
    }
}
